Back to Article
Ex1 - Filtering and Sorting Data
Download Notebook

Ex1 - Filtering and Sorting Data

This time we are going to pull data directly from the internet. Special thanks to: https://github.com/justmarkham for sharing the dataset and materials.

Step 1. Import the necessary libraries

In [1]:
import pandas as pd
import numpy as np

Step 2. Import the dataset from this address.

Step 3. Assign it to a variable called chipo.

In [2]:
chipo = pd.read_csv('https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv',sep='\t')
chipo.head()
order_id quantity item_name choice_description item_price
0 1 1 Chips and Fresh Tomato Salsa NaN $2.39
1 1 1 Izze [Clementine] $3.39
2 1 1 Nantucket Nectar [Apple] $3.39
3 1 1 Chips and Tomatillo-Green Chili Salsa NaN $2.39
4 2 2 Chicken Bowl [Tomatillo-Red Chili Salsa (Hot), [Black Beans... $16.98

Step 4. How many products cost more than $10.00?

In [3]:
chipo['item_price'] = chipo['item_price'].apply(lambda x:float(x[1:]))

chipo_4 = chipo.drop_duplicates(['item_name','quantity'])

chipo_4 = chipo_4[chipo_4.quantity == 1]

chipo_4[chipo_4.item_price > 10].item_name.nunique()
12

Step 5. What is the price of each item?

In [4]:
chipo_5 = chipo.drop_duplicates(['item_name'])
chipo_5 = chipo_5[chipo_5.quantity == 1]
chipo_5[['item_name','item_price']]
item_name item_price
0 Chips and Fresh Tomato Salsa 2.39
1 Izze 3.39
2 Nantucket Nectar 3.39
3 Chips and Tomatillo-Green Chili Salsa 2.39
6 Side of Chips 1.69
7 Steak Burrito 11.75
8 Steak Soft Tacos 9.25
10 Chips and Guacamole 4.45
11 Chicken Crispy Tacos 8.75
12 Chicken Soft Tacos 8.75
16 Chicken Burrito 8.49
21 Barbacoa Burrito 8.99
27 Carnitas Burrito 8.99
33 Carnitas Bowl 8.99
34 Bottled Water 1.09
38 Chips and Tomatillo Green Chili Salsa 2.95
39 Barbacoa Bowl 11.75
40 Chips 2.15
44 Chicken Salad Bowl 8.75
54 Steak Bowl 8.99
56 Barbacoa Soft Tacos 9.25
57 Veggie Burrito 11.25
62 Veggie Bowl 11.25
92 Steak Crispy Tacos 9.25
111 Chips and Tomatillo Red Chili Salsa 2.95
168 Barbacoa Crispy Tacos 11.75
186 Veggie Salad Bowl 11.25
191 Chips and Roasted Chili-Corn Salsa 2.39
233 Chips and Roasted Chili Corn Salsa 2.95
237 Carnitas Soft Tacos 9.25
250 Chicken Salad 10.98
263 Canned Soft Drink 1.25
298 6 Pack Soft Drink 6.49
300 Chips and Tomatillo-Red Chili Salsa 2.39
510 Burrito 7.40
520 Crispy Tacos 7.40
554 Carnitas Crispy Tacos 9.25
664 Steak Salad 8.99
674 Chips and Mild Fresh Tomato Salsa 3.00
738 Veggie Soft Tacos 11.25
1132 Carnitas Salad Bowl 11.89
1229 Barbacoa Salad Bowl 11.89
1414 Salad 7.40
1653 Veggie Crispy Tacos 8.49
1694 Veggie Salad 8.49
3750 Carnitas Salad 8.99

Step 6. Sort by the name of the item

In [5]:
chipo_5[['item_name','item_price']].sort_values(by='item_name')
item_name item_price
298 6 Pack Soft Drink 6.49
39 Barbacoa Bowl 11.75
21 Barbacoa Burrito 8.99
168 Barbacoa Crispy Tacos 11.75
1229 Barbacoa Salad Bowl 11.89
56 Barbacoa Soft Tacos 9.25
34 Bottled Water 1.09
510 Burrito 7.40
263 Canned Soft Drink 1.25
33 Carnitas Bowl 8.99
27 Carnitas Burrito 8.99
554 Carnitas Crispy Tacos 9.25
3750 Carnitas Salad 8.99
1132 Carnitas Salad Bowl 11.89
237 Carnitas Soft Tacos 9.25
16 Chicken Burrito 8.49
11 Chicken Crispy Tacos 8.75
250 Chicken Salad 10.98
44 Chicken Salad Bowl 8.75
12 Chicken Soft Tacos 8.75
40 Chips 2.15
0 Chips and Fresh Tomato Salsa 2.39
10 Chips and Guacamole 4.45
674 Chips and Mild Fresh Tomato Salsa 3.00
233 Chips and Roasted Chili Corn Salsa 2.95
191 Chips and Roasted Chili-Corn Salsa 2.39
38 Chips and Tomatillo Green Chili Salsa 2.95
111 Chips and Tomatillo Red Chili Salsa 2.95
3 Chips and Tomatillo-Green Chili Salsa 2.39
300 Chips and Tomatillo-Red Chili Salsa 2.39
520 Crispy Tacos 7.40
1 Izze 3.39
2 Nantucket Nectar 3.39
1414 Salad 7.40
6 Side of Chips 1.69
54 Steak Bowl 8.99
7 Steak Burrito 11.75
92 Steak Crispy Tacos 9.25
664 Steak Salad 8.99
8 Steak Soft Tacos 9.25
62 Veggie Bowl 11.25
57 Veggie Burrito 11.25
1653 Veggie Crispy Tacos 8.49
1694 Veggie Salad 8.49
186 Veggie Salad Bowl 11.25
738 Veggie Soft Tacos 11.25

Step 7. What was the quantity of the most expensive item ordered?

In [6]:
chipo_5.sort_values(by='item_name').tail(1)
order_id quantity item_name choice_description item_price
738 304 1 Veggie Soft Tacos [Tomatillo Red Chili Salsa, [Fajita Vegetables... 11.25

Step 8. How many times was a Veggie Salad Bowl ordered?

In [7]:
len(chipo[chipo.item_name == 'Veggie Salad Bowl'])
18

Step 9. How many times did someone order more than one Canned Soda?

In [8]:
len(chipo[(chipo.item_name == 'Canned Soda') & (chipo.quantity > 1)])
20